package com.cqqs.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.cqqs.common.enums.error.BusinessError;
import com.cqqs.common.exception.BusinessException;
import com.cqqs.controller.dto.req.concat.ConcatPageReq;
import com.cqqs.controller.dto.req.concat.ContactSaveReq;
import com.cqqs.controller.dto.req.concat.ContactUpdateReq;
import com.cqqs.controller.dto.res.concat.ContactRes;
import com.cqqs.mapper.TechConcatMapper;
import com.cqqs.mapper.po.TechContact;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 科技通讯录功能服务
 *
 * @author mjxie
 * @date 2022/09/23
 */
@Service("techConcatService")
public class TechConcatService {

    private final TechConcatMapper techConcatMapper;

    @Autowired
    public TechConcatService(TechConcatMapper techConcatMapper) {
        this.techConcatMapper = techConcatMapper;
    }

    /**
     * 新增通讯录
     *
     * @param contactSaveReq
     */
    public void save(ContactSaveReq contactSaveReq) {
        TechContact techContact = new TechContact();
        techContact.setName(contactSaveReq.getName());
        techContact.setPhone(contactSaveReq.getPhone());
        techContact.setMobile(contactSaveReq.getMobile());
        techContact.setEmail(contactSaveReq.getEmail());
        techContact.setUnit(contactSaveReq.getUnit());
        techContact.setDepartment(contactSaveReq.getDepartment());
        techContact.setRole(contactSaveReq.getRole());
        this.techConcatMapper.insert(techContact);
    }

    /**
     * 更新
     *
     * @param contactUpdateReq
     */
    public void update(ContactUpdateReq contactUpdateReq) {
        Integer id = contactUpdateReq.getId();
        TechContact techContact = this.techConcatMapper.selectById(id);
        if (null == techContact) {
            throw new BusinessException(ConcatErrorEnum.DATA_NOT_EXIT);
        }
        techContact.setName(contactUpdateReq.getName());
        techContact.setPhone(contactUpdateReq.getPhone());
        techContact.setMobile(contactUpdateReq.getMobile());
        techContact.setEmail(contactUpdateReq.getEmail());
        techContact.setUnit(contactUpdateReq.getUnit());
        techContact.setDepartment(contactUpdateReq.getDepartment());
        techContact.setRole(contactUpdateReq.getRole());
        this.techConcatMapper.updateById(techContact);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     */
    public void deleteByBatch(Integer[] ids) {
        if (ObjectUtils.isNotNull(ids)) {
            List<Integer> integers = Arrays.asList(ids);
            this.techConcatMapper.deleteBatchIds(integers);
        }
    }


    /**
     * 通过id查通讯录信息
     *
     * @param id
     * @return
     */
    public ContactRes findById(Integer id) {
        TechContact techContact = this.techConcatMapper.selectById(id);
        if (null == techContact) {
            throw new BusinessException(ConcatErrorEnum.DATA_NOT_EXIT);
        }
        ContactRes contactRes = buildContactRes(techContact);
        return contactRes;
    }

    /**
     * 分页查询
     *
     * @param concatPageReq
     * @return
     */
    public List<ContactRes> findByConcatPageReq(ConcatPageReq concatPageReq) {
        List<ContactRes> result = new ArrayList<>();
        LambdaQueryWrapper<TechContact> query = new LambdaQueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(concatPageReq.getName())) {
            query.like(TechContact::getName, concatPageReq.getName());
        }
        if (!StringUtils.isNullOrEmpty(concatPageReq.getMobile())) {
            query.like(TechContact::getMobile, concatPageReq.getMobile());
        }
        if (!StringUtils.isNullOrEmpty(concatPageReq.getUnit())) {
            query.like(TechContact::getUnit, concatPageReq.getUnit());
        }
        if (!StringUtils.isNullOrEmpty(concatPageReq.getDepartment())) {
            query.like(TechContact::getDepartment, concatPageReq.getDepartment());
        }
        if (!StringUtils.isNullOrEmpty(concatPageReq.getRole())) {
            query.like(TechContact::getRole, concatPageReq.getRole());
        }
        List<TechContact> techContacts = this.techConcatMapper.selectList(query);
        if (ObjectUtils.isNotNull(techContacts)) {
            techContacts.forEach(contact -> {
                ContactRes contactRes = buildContactRes(contact);
                result.add(contactRes);
            });
        }

        return result;
    }

    /**
     * po转换
     *
     * @param techContact
     * @return
     */
    private ContactRes buildContactRes(TechContact techContact) {
        ContactRes contactRes = new ContactRes();
        contactRes.setId(techContact.getId());
        contactRes.setName(techContact.getName());
        contactRes.setPhone(techContact.getPhone());
        contactRes.setMobile(techContact.getMobile());
        contactRes.setEmail(techContact.getEmail());
        contactRes.setUnit(techContact.getUnit());
        contactRes.setDepartment(techContact.getDepartment());
        contactRes.setRole(techContact.getRole());
        return contactRes;
    }


    /**
     * 联系人错误枚举
     */
    enum ConcatErrorEnum implements BusinessError {


        DATA_NOT_EXIT(10000, "联系人不存在");


        private int code;

        private String msg;

        ConcatErrorEnum(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        @Override
        public int getCode() {
            return code;
        }

        @Override
        public String getMsg() {
            return msg;
        }

        @Override
        public String getData() {
            return null;
        }
    }
}
